package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Inventory;
import com.sjlh.centurylianhua.entity.Sale;
import com.sjlh.centurylianhua.entity.SaleList;
import com.sjlh.centurylianhua.mapper.InventoryMapper;
import com.sjlh.centurylianhua.mapper.SaleListMapper;
import com.sjlh.centurylianhua.mapper.SaleMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;


@CrossOrigin
@RestController
@RequestMapping("/sale")
public class SaleController {
    @Resource
    private SaleMapper saleMapper;
    @Resource
    private SaleListMapper saleListMapper;

    /**
     * 添加销售日志
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody Sale sale){
        //自动生存订单号
        sale.setCodeId("SJLH"+new Date().getTime());
        long r=saleMapper.insert(sale);
        //存储id返回给前端方便添加销售日志详情
        long saleId=sale.getSaleId();
        if (r==1) {
            return new ResultData(true, 20000, "添加成功！", true, saleId);
        }
        else
            return new ResultData(true,20000,"添加失败！",false,null);
    }
    @RequestMapping("/del")
    public Object del(@RequestBody Sale sale){
        int r=saleMapper.deleteById(sale.getSaleId());
        if(r==1)
            return new ResultData(true,20000,"删除成功！",true,null);
        else
            return new ResultData(true,20000,"删除失败！",false,null);


    }

    /**
     * 查询所有销售日志
     * @param searchCon
     * @return
     */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon){
        QueryWrapper<Sale> wrapper = new QueryWrapper<Sale>();
        //当条件不为空时，添加where条件
        wrapper.eq(searchCon.getTypeName()!=null,"sale.customerId",searchCon.getTypeName());
        wrapper.eq(searchCon.getTypeName2()!=null,"sale.remarks2",searchCon.getTypeName2());
        wrapper.eq(searchCon.getTypeName3()!=null,"sale.state",searchCon.getTypeName3());
        wrapper.ge(StringUtils.isnotNullObj(searchCon.getBeginDate()),"saleTime",searchCon.getBeginDate());
        wrapper.le(StringUtils.isnotNullObj(searchCon.getEndDate()),"saleTime",searchCon.getEndDate());
        //追加多表连接的sql语句
        wrapper.apply("depot.id = sale.remarks2 and sale.customerId= customer.customerId and sale.userId=user.id");
        //不分页时查询所有销售日志
        if(searchCon.getPage()==null){
            List<Sale> list = saleMapper.getInfo(wrapper);
            long count = saleMapper.selectList(wrapper).size();
            return new ResultData(true,20000,"成功",list,count);
        }else {
            // 封装分页对象
            Page<Sale> page = new Page<Sale>(searchCon.getPage(), searchCon.getLimit());
            Page<Sale> depotPage = saleMapper.getInfoPage(page, wrapper);
            List<Sale> list=depotPage.getRecords();//所有数据
            long count=depotPage.getTotal();//总条数
            return new ResultData(true,20000,"成功",list,count);
        }
    }
    /**
     * 修改销售日志状态并在出库时修改库存
     * */

      @RequestMapping("/set")
    public Object setRemark(@RequestBody Sale sale){
          Sale s=new Sale();
          UpdateWrapper<Sale> wrapper= new UpdateWrapper<>();
          wrapper.set("state",sale.getState());
          wrapper.eq("saleId",sale.getSaleId());
          long r = saleMapper.update(s,wrapper);
          if(r==1){
              //判断是否通过申请
              if(sale.getState()==2 ) {
                  //在修改成功后执行修改库存操作
                  //查询一下商品销售日志
                  QueryWrapper<SaleList> selectSale = new QueryWrapper<>();
                  selectSale.eq("saleId",sale.getSaleId());
                  //查询销售日志详情表中的商品和销售的数量
                  List<SaleList> list = saleListMapper.selectList(selectSale);
              }return new ResultData(true,20000,"修改成功",true,null);
          }else{
              return new ResultData(false,20001,"修改失败",false,null);
          }
      }
    //销售统计图
    @RequestMapping("/saleCount")
    public Object saleCount(@RequestBody SearchCon searchCon){
        List<Object> obj=new ArrayList<Object>();
        List<String> name=new ArrayList<String>();
        List<Integer> intlist=new ArrayList<Integer>();
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        Map<String,Object> map=new HashMap<String,Object>();
        wrapper.apply("user.id=sale.userId");
        //时间查询
        wrapper.ge(StringUtils.isnotNullObj(searchCon.getBeginDate()),"saleTime",searchCon.getBeginDate());
        wrapper.le(StringUtils.isnotNullObj(searchCon.getEndDate()),"saleTime",searchCon.getEndDate());
        if(searchCon.getTypeName()!=null && searchCon.getTypeName()==1){
            wrapper.apply("year(sale.saleTime)= year(now())");
        }
        List<Sale> sale=saleMapper.saleCount(wrapper);
        for(Sale sa:sale){
            name.add(sa.getUserName());
            intlist.add(Integer.valueOf(sa.getRemarks3()));
        }
        map.put("x",name);
        map.put("y",intlist);
        return new ResultData(true,20000,"成功",map,0L);
    }
}
